//
//  UserDataModel.m
//  CoolTennisBall
//
//  Created by Coollang on 16/9/9.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserDataModel.h"
#import "NSString+Extension.h"


@implementation UserMonthDataModel


- (instancetype)initWithYear:(NSString *)yearStr withDict:(NSDictionary *)dict {
    if (self = [super init]) {
        self.year = yearStr;
        __weak typeof(self) weakSelf = self;
        [dict enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            
            if ([key isEqualToString:@"01"]) {
                weakSelf.JanNum = [obj integerValue];
            }else if ([key isEqualToString:@"02"]){
                weakSelf.FebNum = [obj integerValue];
            }else if ([key isEqualToString:@"03"]){
                weakSelf.MarNum = [obj integerValue];
            }else if ([key isEqualToString:@"04"]){
                weakSelf.AprNum = [obj integerValue];
            }else if ([key isEqualToString:@"05"]){
                weakSelf.MayNum = [obj integerValue];
            }else if ([key isEqualToString:@"06"]){
                weakSelf.JuneNum = [obj integerValue];
            }else if ([key isEqualToString:@"07"]){
                weakSelf.JulyNum = [obj integerValue];
            }else if ([key isEqualToString:@"08"]){
                weakSelf.AugNum = [obj integerValue];
            }else if ([key isEqualToString:@"09"]){
                weakSelf.SepNum = [obj integerValue];
            }else if ([key isEqualToString:@"10"]){
                weakSelf.OctNum = [obj integerValue];
            }else if ([key isEqualToString:@"11"]){
                weakSelf.NovNum = [obj integerValue];
            }else if ([key isEqualToString:@"12"]){
                weakSelf.DecNum = [obj integerValue];
            }
            
        }];
        
    }
    return self;
}
// 计算虚线的值
- (NSArray <NSString *>*)dashLineTargetValue {
    
    // 取出月份中的最大值
    NSInteger maxNum = [[self maxNumber] integerValue];
    maxNum += 1000;
    maxNum = maxNum - maxNum % 1000;
    
    return @[@(0).stringValue, @(maxNum / 5).stringValue,@(maxNum * 2/ 5).stringValue,@(maxNum * 3/ 5).stringValue,@(maxNum *4/ 5).stringValue, @(maxNum).stringValue];
    
}

- (NSInteger)maxYValue {
    NSInteger maxNum = [[self maxNumber] integerValue];
    maxNum += 1000;
    maxNum = maxNum - maxNum % 1000;
    
    return maxNum;
}

// 取出月份中的最大值
- (NSNumber *)maxNumber {
    
    NSArray *array = [self.yearMonthDatas sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:nil ascending:YES]]];
    
    return array.lastObject;
}

- (NSArray<NSNumber *> *)barYValus {
    if (_barYValus == nil) {
        
        NSInteger maxNum = [[self maxNumber] integerValue];
        maxNum += 1000;
        maxNum = maxNum - maxNum % 1000;
        NSMutableArray *arrM = [NSMutableArray array];
        for (NSNumber *number in self.yearMonthDatas) {
            
            CGFloat scale = number.floatValue / (maxNum * 1.0);
            [arrM addObject:@(scale)];
        }
        
        _barYValus = arrM.copy;
    }
    return _barYValus;
}

- (NSArray <NSNumber *>*)yearMonthDatas {
    if (_yearMonthDatas == nil) {
        _yearMonthDatas = @[@(self.JanNum),@(self.FebNum),@(self.MarNum),@(self.AprNum),@(self.MayNum),@(self.JuneNum),@(self.JulyNum),@(self.AugNum),@(self.SepNum),@(self.OctNum),@(self.NovNum),@(self.DecNum)];
    }
    return _yearMonthDatas;
}

@end

@implementation UserBestDataModel



@end

@implementation UserBestListModel

- (instancetype)initWithBestTime:(NSString *)timeStr withArray:(NSArray *)array {
    
    if (self = [super init]) {
        self.bestTime = [NSString checkIfNullWithString:timeStr];
        NSMutableArray *arrM = [NSMutableArray array];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if (obj.allKeys.count == 1) {
                
                // 取出Key值
                NSString *key = obj.keyEnumerator.nextObject;
                
                UserBestDataModel *bestModel = [[UserBestDataModel alloc]init];
                bestModel.bestValueStr = obj[key];
                
                bestModel.bestTime = [NSString checkIfNullWithString:timeStr];
                
                BOOL isChinese = [NSBundle currentLanguageIsChinese];
                if ([key isEqualToString:@"MaxServeSpeed"]) {
                    bestModel.bestDataName = isChinese ? @"发球速度" : @"Serve Speed";
                    bestModel.unitString = @"km/h";
                }else if ([key isEqualToString:@"Duration"]){
                    bestModel.bestDataName = NSLocalizedString(@"User.Active Time", @"活跃时间");
                    bestModel.unitString = NSLocalizedString(@"Unit.Minute", @"分钟");
                }else if ([key isEqualToString:@"TotalCount"]){
                    bestModel.bestDataName = NSLocalizedString(@"Shots", @"击球次数");
                    bestModel.unitString = isChinese ?@"次":@"times";
                    
                }else if ([key isEqualToString:@"TotalScore"]){
                    bestModel.bestDataName =  NSLocalizedString(@"Motor Score", @"运动评分");
                    bestModel.unitString = NSLocalizedString(@"Report.Unit.Points", @"分");
                }
                
                [arrM addObject:bestModel];
                
            }
        }];
        
        self.bestDataList = arrM.copy;
    }
    return self;
}

@end

@implementation UserDataModel

- (void)parseUserDataDictionary:(NSDictionary *)dictionary {
    
    self.TotalDuration = [NSString checkIfNullWithString:dictionary[@"TotalDuration"]];
    self.TotalCount = [NSString checkIfNullWithString:dictionary[@"TotalCount"]];
    NSDictionary *yearMonthDict = dictionary[@"YearMonthList"];
    NSMutableArray *arrM = [NSMutableArray array];
    
    if ([yearMonthDict isKindOfClass:[NSDictionary class]]) {
        [yearMonthDict enumerateKeysAndObjectsUsingBlock:^(NSString *_Nonnull key, NSDictionary *_Nonnull obj, BOOL * _Nonnull stop) {
            if ([key integerValue] > 2014) {
                UserMonthDataModel *monthsModel = [[UserMonthDataModel alloc] initWithYear:key withDict:obj];
                [arrM addObject:monthsModel];
            }
        }];
    }
    
    NSDictionary *bestList = dictionary[@"BestList"];
    NSMutableArray *bestListArrM = [NSMutableArray array];
   
    if ([bestList isKindOfClass:[NSDictionary class]]) {
       
        [bestList enumerateKeysAndObjectsUsingBlock:^(NSString *_Nonnull key, NSArray *_Nonnull obj, BOOL * _Nonnull stop) {
            UserBestListModel *listModel = [[UserBestListModel alloc] initWithBestTime:key withArray:obj];
            [bestListArrM addObject:listModel];
        }];
    }
    
    self.bestList = bestListArrM.copy;
    // 对数据进行排序
    [arrM sortUsingComparator:^NSComparisonResult(UserMonthDataModel *_Nonnull obj1, UserMonthDataModel *_Nonnull obj2) {
       return [obj1.year compare:obj2.year];
    }];
    
    self.yearMonthList = arrM.copy;
    self.AvgList = dictionary[@"AvgList"];
    self.UserInfo = dictionary[@"UserInfo"];
    
}

@end



